import random
import time

def random_number():
    """
    随机生成四个整数
    :return:
    """
    y = []
    for i in range(4):
        y.append(random.randint(1,13))
    return y
# def fun(k,l,i):
#     """
#     两个数据之间的运算
#     :param k:
#     :param l:
#     :param i:
#     :return:
#     """
#     if i==0:
#         tem = k+l
#         return tem
#     elif i==1:
#         tem = k-l
#         return tem
#     elif i==2:
#         tem = k*l
#         return tem
#     elif i==3:
#         if l==0:
#             return
#         else:
#             tem = k/l
#             return tem
def fun(k, l, i):
    """
    两个数据之间的运算
    :param k:
    :param l:
    :param i:
    :return:
    """
    if i==0:
        if (type(k) is int or type(k) is float) and (type(l) is int or type(l) is float):
            tem = k + l
            return tem
        else:
            return
    elif i==1:
        if (type(k) is int or type(k) is float) and (type(l) is int or type(l) is float):
            tem = k - l
            return tem
        else:
            return
    elif i==2:
        if (type(k) is int or type(k) is float) and (type(l) is int or type(l) is float):
            tem = k * l
            return tem
        else:
            return
    elif i==3:
        if (type(k) is int or type(k) is float) and (type(l) is int or type(l) is float):
            if l != 0:
                tem = k / l
                return tem
            else:
                return
        else:
            return


def poker(y):
    """
    将生成的随机数以扑克牌的形式输出
    :param y:
    :return:
    """
    y1 = y.copy()
    while 1 in y1 or 11 in y1 or 12 in y1 or 13 in y1:
        if 1 in y1:
            z = y1.index(1)
            y1.remove(1)
            y1.insert(z, 'A')
        if 11 in y1:
            z = y1.index(11)
            y1.remove(11)
            y1.insert(z, 'J')
        if 12 in y1:
            z = y1.index(12)
            y1.remove(12)
            y1.insert(z, 'Q')
        if 13 in y1:
            z = y1.index(13)
            y1.remove(13)
            y1.insert(z, 'K')
        continue
    # if 1 in y1:
    #     z = y1.index(1)
    #     y1.remove(1)
    #     y1.insert(z, 'A')
    # if 11 in y1:
    #     z = y1.index(11)
    #     y1.remove(11)
    #     y1.insert(z, 'J')
    # if 12 in y1:
    #     z = y1.index(12)
    #     y1.remove(12)
    #     y1.insert(z, 'Q')
    # if 13 in y1:
    #     z = y1.index(13)
    #     y1.remove(13)
    #     y1.insert(z, 'K')
    print("随机抽取的四张牌为：", y1[0], y1[1], y1[2], y1[3])


# y = random_number()
# print(y)
# y = [7, 1, 11, 6]
# x = ['+', '-', '*', '/']


def calculate(y):
    """
    计算，求出符合题目要求的
    :return:
    """
    result_list = []
    result_list2 = []
    temp = 0
    temp1 = 0
    temp2 = 0
    for i in range(0,4):
        for j in range(0,4):
            for k in range(0,4):
                for l in range(0,4):
                    if i == j or i == k or i == l or j == k or j == l or k == l:
                        continue
                    else:
                        for n in range(0,4):
                            for m in range(0,4):
                                for p in range(0,4):
                                    for q in range(0,6):
                                        result = 0
                                        if q == 0:
                                            temp1 = fun(y[i], y[j], n)
                                            temp1 = fun(temp1, y[k], m)
                                            result = fun(temp1, y[l], p)
                                            if result==24:
                                                result_list.append(
                                                    "(({0}{1}{2}){3}{4}){5}{6}=24".format(y[i], x[n], y[j], x[m], y[k],
                                                                                          x[p], y[l]))
                                                result_list2.append(
                                                    "(({0}{1}{2}){3}{4}){5}{6}".format(y[i], x[n], y[j], x[m], y[k],
                                                                                       x[p], y[l]))
                                                # print("(({0}{1}{2}){3}{4}){5}{6}=24".format(y[i], x[n], y[j], x[m], y[k],
                                                #                                             x[p], y[l]))
                                                temp += 1
                                            #break
                                            continue
                                        elif q == 1:
                                            temp1 = fun(y[i], y[j], n)
                                            temp2 = fun(y[k], y[l], p)
                                            result = fun(temp1, temp2, m)
                                            if result == 24:
                                                result_list.append(
                                                    "({0}{1}{2}){3}({4}{5}{6})=24".format(y[i], x[n], y[j], x[m], y[k],
                                                                                          x[p], y[l]))
                                                result_list2.append(
                                                    "({0}{1}{2}){3}({4}{5}{6})".format(y[i], x[n], y[j], x[m], y[k],
                                                                                       x[p], y[l]))
                                                # print("({0}{1}{2}){3}({4}{5}{6})=24".format(y[i], x[n], y[j], x[m], y[k],
                                                #                                             x[p], y[l]))
                                                temp += 1
                                            #break
                                            continue
                                        elif q == 2:
                                            temp1 = fun(y[j], y[k], m)
                                            temp1 = fun(y[i], temp1, n)
                                            result = fun(temp1, y[l], p)
                                            if result == 24:
                                                result_list.append(
                                                    "({0}{1}({2}{3}{4})){5}{6}=24".format(y[i], x[n], y[j], x[m], y[k],
                                                                                          x[p], y[l]))
                                                result_list2.append(
                                                    "({0}{1}({2}{3}{4})){5}{6}".format(y[i], x[n], y[j], x[m], y[k],
                                                                                       x[p], y[l]))
                                                # print("({0}{1}({2}{3}{4})){5}{6}=24".format(y[i], x[n], y[j], x[m], y[k],
                                                #                                             x[p], y[l]))
                                                temp += 1
                                            # break
                                            continue
                                        # elif q==3:
                                        #     temp1 = fun(y[k], y[l], m)
                                        #     temp2 = fun(y[i], y[j], p)
                                        #     result = fun(temp1, temp2, n)
                                        #     if result == 24:
                                        #
                                        #         print("({0}{1}{2}){3}({4}{5}{6})=24".format(y[i], x[n], y[j], x[m], y[k],
                                        #                                                     x[p], y[l]))
                                        #         temp += 1
                                        #         break
                                        elif q == 3:
                                            temp1 = fun(y[j], y[k], m)
                                            temp1 = fun(temp1, y[l], p)
                                            result = fun(y[i], temp1, n)
                                            if result == 24:
                                                result_list.append(
                                                    "{0}{1}(({2}{3}{4}){5}{6})=24".format(y[i], x[n], y[j], x[m], y[k],
                                                                                          x[p], y[l]))
                                                result_list2.append(
                                                    "{0}{1}(({2}{3}{4}){5}{6})".format(y[i], x[n], y[j], x[m], y[k],
                                                                                       x[p], y[l]))
                                                # print("{0}{1}(({2}{3}{4}){5}{6})=24".format(y[i], x[n], y[j], x[m], y[k],
                                                #                                             x[p], y[l]))
                                                temp += 1
                                            #break
                                            continue
                                        elif q == 4:
                                            temp1 = fun(y[k], y[l], p)
                                            temp1 = fun(y[j], temp1, m)
                                            result = fun(y[i], temp1, n)
                                            if result == 24:
                                                result_list.append(
                                                    "{0}{1}({2}{3}({4}{5}{6}))=24".format(y[i], x[n], y[j], x[m], y[k],
                                                                                          x[p], y[l]))
                                                result_list2.append(
                                                    "{0}{1}({2}{3}({4}{5}{6}))".format(y[i], x[n], y[j], x[m], y[k],
                                                                                       x[p], y[l]))
                                                # print("{0}{1}({2}{3}({4}{5}{6}))=24".format(y[i], x[n], y[j], x[m], y[k],
                                                #                                             x[p], y[l]))
                                                temp += 1
                                            #break
                                            continue
    return temp, result_list, result_list2
    # if temp == 0:
    #     print("这四张牌无法完成24点游戏")
    # else:
    #     print()


def timer():
    """
    计时器
    :return:
    """
    return time.perf_counter()


def player():
    """
    玩家进行游戏
    :return:
    """
    hit_point = 3
    score = 0
    print("==================================================")
    print("==                     24点游戏                  ==")
    print("==游戏玩家开始时的血量为:{0}                          ==".format(hit_point))
    print("==游戏玩家开始时的分数为:{0}                          ==".format(score))
    print("==是否开始游戏:Y or N                             ==")
    print("==================================================")
    use = input()
    while use == 'Y' or 'y':
        y = random_number()
        poker(y)
        result, result_list, result_list2 = calculate(y)
        if result == 0:
            print("这四个数无法完成24点游戏，生命值扣一")
            hit_point -= 1
            if hit_point !=0:
                print("你当前的生命值为：{0}  是否继续：Y/N".format(hit_point))
                use = input()
            else:
                print("生命值为0，游戏结束")
                break
            # break

            continue
        elif result != 0:
            print("请玩家输入正确表达式：（A为1，J为11，Q为12，K为13）")
            # for i in result_list:
            #     print(i)
            start_time = timer()
            player_expression = input()
            end_time = timer()
            if end_time-start_time <= 100000000:
                if player_expression in result_list2:
                    print("回答正确，加十分")
                    score += 10
                    print("这四张牌所有的组合为：")
                    for i in result_list:
                        print("{0}".format(i))
                    print("是否继续：Y/N")
                    use = input()
                else:
                    print("回答错误：生命值扣一")
                    hit_point -= 1
                    if hit_point != 0:
                        print("你当前的生命值为：{0} 得分为{1}".format(hit_point, score))
                        print("这四张牌所有的组合为：")
                        for i in result_list:
                            print("{0}".format(i))
                        print("是否继续：Y/N")
                        use = input()
                    else:
                        print("生命值为0，游戏结束")
                        break
                    # print("这四张牌所有的组合为：")
                    # for i in result_list:
                    #     print("{0}".format(i))
                    # print("是否继续：Y/N")
                    # use = input()
            else:
                print("未在规定时间内答完，生命值减一")
                hit_point -= 1
                print("是否继续：Y/N")
                use = input()
            continue
                # continue
    print("你最后的生命值为：{0}；最终得分为：{1}".format(hit_point, score))

# y = random_number()
# # y = [5,9,3,7]
# x = ['+', '-', '*', '/']
# poker(y)
# calculate(y)

x = ['+', '-', '*', '/']
# def player():
#     hit_point = 1
#     score = 0
#     y = random_number()
#     poker(y)
#     result, result_list, result_list2 = calculate(y)
#     if result == 0:
#         print("这四个数无法完成24点游戏，生命值扣一")
#         hit_point -= 1
#         if hit_point !=0:
#             print("你当前的生命值为：{0}  是否继续：Y/N".format(hit_point))
#             use = input()
#         else:
#             print("生命值为0，游戏结束")


player()
